package core;

import Index.Index;
import Index.TreeNode;
import JitUtils.FileHash;
import gitobject.Blob;
import gitobject.Tree;
import repository.Repository;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author shkstart
 * @create 2021-11-16 19:29
 */
public class JitAdd {
        public static boolean add(String path) throws Exception {
        Index index = new Index();
        TreeNode indexTree = index.getIndexTree();
        File file = new File(path);
        if(file.isFile()) {
            TreeNode treeNode = addFile(path);

            index = index.readObject();

            if(indexTree.getType() == null){
                index.setIndexTree(treeNode);
            }else if(indexTree != treeNode){
                index.setIndexTree(index.updateIndex(treeNode, indexTree));
            }

            index.writeObject();
            return true;
        }else{
            TreeNode treeNode = addDic(path);
            index = index.readObject();
            if(indexTree.getType() == null){
                index.setIndexTree(treeNode);
            }else if(indexTree != treeNode){
                index.setIndexTree(index.updateIndex(treeNode, indexTree));
            }
            index.writeObject();
        }

        return false;
    }

    public static TreeNode addFile(String path) throws IOException {
        TreeNode temp = FileHash.fileHash(new File(path),"1");
        OutputStream os = null;
        try {
            File f = new File(path);
            Blob blob = new Blob(f,"1");
            String key = blob.getKey();
            String dicOfBlob = new File(".").getCanonicalPath() + File.separator + ".jit" +  File.separator + "objects" + File.separator + key.substring(0,2);
            String fileOfBlob = new File(".").getCanonicalPath() + File.separator + ".jit" + File.separator + "objects" + File.separator + key.substring(0,2) + File.separator + key.substring(2, key.length());
            File dir = new File(dicOfBlob);
            File file = new File(fileOfBlob);
            if(!file.exists()){
                dir.mkdir();
                file.createNewFile();
                blob.writeObject(fileOfBlob);
            }
            return temp;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(os != null)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }

    public static TreeNode addDic(String path) {
        try {
            TreeNode dicNode = FileHash.hashOfDic(new File(path));
            File file = new File(path);
            String[] content = file.list();//取得当前目录下所有文件和文件夹
            if(content == null){
                return null;
            }
            for (String name : content) {
                String tem = path + File.separator + name;
                File temp = new File(tem);
                if (temp.isFile()) {
                    addFile(tem);

                } else {
                    addDic(tem);
                }
            }
            return dicNode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

